Trò chơi đua xe động vật trong UNITY Engine
114.728 lượt xem;
- GoldAddition.cs
- GameScreen /
- AnimalRacing /
- Scripts /
- Assets /
- project /
1 using UnityEngine;
2 using System.Collections.Generic;
3
4 public class GoldAddition : MonoBehaviour {
5
6 private List<GameObject> coins;
7
8 private float duration;
9
10 private int numberCoins1;
11 private int numberCoins2;
12
13 private float stateTime1;
14 private float stateTime2;
15
16 private float destX;
17 private float destY;
18
19 public void Start()
20 {
21 duration = 0.05f;
22 coins = new List<GameObject>();
23 destX = -2;
24 destY = 1.7f;
25 }
26
27 //-2, 1.7
28
29 public void addGold1(int gold)
30 {
31 numberCoins1 += gold;
32 /*
33 for (int i = 0; i < gold; i++)
34 {
35 addGold(1.2f, 1.2f);
36 }
37 * */
38 }
39
40 public void addGold2(int gold)
41 {
42 /*
43 for (int i = 0; i < gold; i++)
44 {
45 addGold(1.2f, 0.6f);
46 }
47 * */
48 numberCoins2 += gold;
49 }
50
51 public void Update()
52 {
53 if (numberCoins1 > 0)
54 {
55 stateTime1 += Time.deltaTime;
56 if (stateTime1 >= duration)
57 {
58 addGold(1.2f, 1.2f);
59 numberCoins1--;
60 }
61 }
62
63 if (numberCoins2 > 0)
64 {
65 stateTime2 += Time.deltaTime;
66 if (stateTime2 >= duration)
67 {
68 addGold(1.2f, 0.6f);
69 numberCoins2--;
70 }
71 }
72 }
73
74 private void addGold(float x, float y)
75 {
76 GameObject gObject = (GameObject)Instantiate(Resources.Load<GameObject>("Add/Gold"));
77 gObject.transform.parent = gameObject.transform;
78 gObject.transform.localPosition = new Vector3(x, y, 0);
79 coins.Add(gObject);
80
81 int ra = Random.Range(0, 1);
82 if (ra == 0)
83 {//Tren
84 gObject.AddComponent<Bezier>().setBezier(2f,
85 new Vector2(x, y),
86 new Vector2(x, y + Random.Range(0.1f, 1.5f)),
87 new Vector2(destX + Random.Range(0.1f, 1.5f), y + Random.Range(0.1f, 1.5f)),
88 new Vector2(destX, destY));
89 }
90 else
91 {//Duoi
92 gObject.AddComponent<Bezier>().setBezier(2f,
93 new Vector2(x, y),
94 new Vector2(x, y - Random.Range(0.1f, 1.5f)),
95 new Vector2(destX + Random.Range(0.1f, 1.5f), y - Random.Range(0.1f, 1.5f)),
96 new Vector2(destX, destY));
97 }
98 Destroy(gObject, 2);
99 }
100 }
2 using System.Collections.Generic;
3
4 public class GoldAddition : MonoBehaviour {
5
6 private List<GameObject> coins;
7
8 private float duration;
9
10 private int numberCoins1;
11 private int numberCoins2;
12
13 private float stateTime1;
14 private float stateTime2;
15
16 private float destX;
17 private float destY;
18
19 public void Start()
20 {
21 duration = 0.05f;
22 coins = new List<GameObject>();
23 destX = -2;
24 destY = 1.7f;
25 }
26
27 //-2, 1.7
28
29 public void addGold1(int gold)
30 {
31 numberCoins1 += gold;
32 /*
33 for (int i = 0; i < gold; i++)
34 {
35 addGold(1.2f, 1.2f);
36 }
37 * */
38 }
39
40 public void addGold2(int gold)
41 {
42 /*
43 for (int i = 0; i < gold; i++)
44 {
45 addGold(1.2f, 0.6f);
46 }
47 * */
48 numberCoins2 += gold;
49 }
50
51 public void Update()
52 {
53 if (numberCoins1 > 0)
54 {
55 stateTime1 += Time.deltaTime;
56 if (stateTime1 >= duration)
57 {
58 addGold(1.2f, 1.2f);
59 numberCoins1--;
60 }
61 }
62
63 if (numberCoins2 > 0)
64 {
65 stateTime2 += Time.deltaTime;
66 if (stateTime2 >= duration)
67 {
68 addGold(1.2f, 0.6f);
69 numberCoins2--;
70 }
71 }
72 }
73
74 private void addGold(float x, float y)
75 {
76 GameObject gObject = (GameObject)Instantiate(Resources.Load<GameObject>("Add/Gold"));
77 gObject.transform.parent = gameObject.transform;
78 gObject.transform.localPosition = new Vector3(x, y, 0);
79 coins.Add(gObject);
80
81 int ra = Random.Range(0, 1);
82 if (ra == 0)
83 {//Tren
84 gObject.AddComponent<Bezier>().setBezier(2f,
85 new Vector2(x, y),
86 new Vector2(x, y + Random.Range(0.1f, 1.5f)),
87 new Vector2(destX + Random.Range(0.1f, 1.5f), y + Random.Range(0.1f, 1.5f)),
88 new Vector2(destX, destY));
89 }
90 else
91 {//Duoi
92 gObject.AddComponent<Bezier>().setBezier(2f,
93 new Vector2(x, y),
94 new Vector2(x, y - Random.Range(0.1f, 1.5f)),
95 new Vector2(destX + Random.Range(0.1f, 1.5f), y - Random.Range(0.1f, 1.5f)),
96 new Vector2(destX, destY));
97 }
98 Destroy(gObject, 2);
99 }
100 }
-2, 1.7
{Tren
{Duoi